<?php
require_once APP . '/application/dailicontrollers/AdminAction.php';
require_once APP . '/application/models/SfdataModel.php';
require_once APP . '/application/models/Util.php';

/**
 * 服务器列表管理
 * @package admin
 */
class ServerController extends AdminAction
{
    
    public function init()
    {
        // $this->_config = Registry::get ('config');
    }


    public function addAction()
    {    	
    	$admininfo = DailiUser::getLoginInfo();
    	
    	$row = $this->db->fetchRow("select * from admin where id=?", array($admininfo['id']));
    	$today = strtotime('0 day 00:00:00');
    	if ($row['dayline'] != $today){
    		$row['pubnum'] = 0;
    	}
    	
    	if ($row['pubnum'] >= $row['publimit']){
    		$this->view->check = 0;
    	}else{
    		$this->view->check = 1;
    	}
    	
        //加载默认视图
    }
    
    public function doaddAction()
    {
    	$params = $this->getAllParams();
    	
    	// 时间处理
    	$params['sdatetime'] = $params['sdatetime_day'] . ' ' . $params['sdatetime_hour'] . ':' . $params['sdatetime_minute'];
    	$params['pubtime'] = time();
    	$params['pubip'] = Util::getip(true);
    	
    	$admininfo = DailiUser::getLoginInfo();
    	$params['adminid'] = $admininfo['id'];
    	
    	$row = $this->db->fetchRow("select * from admin where id=?", array($admininfo['id']));
    	$today = strtotime('0 day 00:00:00');
    	if ($row['dayline'] != $today){
    		$row['pubnum'] = 0;
    	}
    	
    	if ($row['pubnum'] >= $row['publimit'] && $params['ischeck']){
    		$this->view->msg = '添加失败！达到每日发布上限,你只能添加待审核的广告！';
    		$this->view->url = $this->url($this->view->baseUrl, "server", "add");
    		$this->showmessage();
    		exit;
    	}
    	
    	// 如果审核通过，则添加
    	if ($params['ischeck']){
    		$this->db->update("admin", array("dayline" => $today, "pubnum" => $row['pubnum'] + 1), 'id=' . $admininfo['id']);
    	}
    	
    	$model = new SfdataModel($this->db);
    	$model->add($params);
    	
    	$this->view->msg = '添加成功！';
    	$this->view->url = $this->url($this->view->baseUrl, "server", "add");
    	$this->showmessage();
    }
    
    public function listAction()
    {
    	$nowp = (int)$this->getParam("nowp", 1);
    	$per_page = 20;
    	$where = $this->getParam("where");
    	
    	$model = new SfdataModel($this->db);    	
    	
    	$admininfo = DailiUser::getLoginInfo();    	
    	$result = $model->fetchDailiAll($admininfo['id'], $nowp, $per_page, $where);
    	
    	$this->view->list = $result['list'];
    	$this->view->page = $result['page'];
    	$this->view->nowp = $nowp;
    	$this->view->where = $where;
    }
        
    public function batchAction()
    {
    	$ids = $this->getParam("ids");
    	$opp = $this->getParam("type");
    	
    	if ($opp == 'del'){
    		$this->db->delete("sfdata", "where id in(" . implode(",", $ids) . ')');
    	}else{
    		if (in_array($opp, array("isbest", "ishot", "ischeck"))){
    			$this->db->update("sfdata", array($opp => 1),  "where id in(" . implode(",", $ids) . ')');
    		}
    	}    	
    }
}
